package com.wing.wechat;

import com.alibaba.fastjson.JSONObject;
import com.wing.utils.HttpsUtils;
import com.wing.wechat.util.WechatUtil;
import com.wing.wechat.vo.*;
import org.springframework.stereotype.Service;

/**
 * 企业微信部门服务实现
 */
@Service
public class WechatDepartmentServiceImpl implements WechatDepartmentService {

    @Override
    public ResponseToken getAccessToken(String secret) {
        String requestUrl = WechatUtil.GET_TOKEN_URL.replace("REQUEST_ID", WechatUtil.ENTERPRISE_ID).replace("REQUEST_SECRET", secret);
        String result = HttpsUtils.httpsGet(requestUrl);
        ResponseToken responseToken = JSONObject.parseObject(result, ResponseToken.class);
        return responseToken;
    }

    @Override
    public ResponseDepartmentList departmentList(Integer departmentId) {
        ResponseToken token = getAccessToken(WechatUtil.ENTERPRISE_SECRET);
        String requestUrl = WechatUtil.LIST_DEPARTMENT_URL.replace("ACCESS_TOKEN", token.getAccess_token());
        if(departmentId != null){
            requestUrl = requestUrl.replace("DEPARTMENT_ID", departmentId + "");
        }
        String departmentListResult = HttpsUtils.httpsGet(requestUrl);
        ResponseDepartmentList departmentList = JSONObject.parseObject(departmentListResult, ResponseDepartmentList.class);
        return departmentList;
    }

    @Override
    public ResponseDepartmentDetail departmentDetail(Integer id) {
        ResponseToken token = getAccessToken(WechatUtil.ENTERPRISE_SECRET);
        String requestUrl = WechatUtil.GET_DEPARTMENT_URL.replace("ACCESS_TOKEN", token.getAccess_token()).replace("DEPARTMENT_ID", id + "");
        String departmentDetailResult = HttpsUtils.httpsGet(requestUrl);
        ResponseDepartmentDetail departmentDetail = JSONObject.parseObject(departmentDetailResult, ResponseDepartmentDetail.class);
        return departmentDetail;
    }

    @Override
    public ResponseCreateDepartment createDepartment(RequestCreateDepartment requestCreateDepartment) {
        ResponseToken token = getAccessToken(WechatUtil.ADDRESS_SECRET);
        String requestUrl = WechatUtil.CREATE_DEPARTMENT_URL.replace("ACCESS_TOKEN", token.getAccess_token());
        String createDepartmentResult = HttpsUtils.httpsPostByJson(requestUrl, JSONObject.toJSONString(requestCreateDepartment));
        ResponseCreateDepartment createDepartment = JSONObject.parseObject(createDepartmentResult, ResponseCreateDepartment.class);
        return createDepartment;
    }

    @Override
    public ResponseUpdateDepartment updateDepartment(RequestUpdateDepartment requestUpdateDepartment) {
        ResponseToken token = getAccessToken(WechatUtil.ADDRESS_SECRET);
        String requestUrl = WechatUtil.UPDATE_DEPARTMENT_URL.replace("ACCESS_TOKEN", token.getAccess_token());
        String updateDepartmentResult = HttpsUtils.httpsPostByJson(requestUrl, JSONObject.toJSONString(requestUpdateDepartment));
        ResponseUpdateDepartment updateDepartment = JSONObject.parseObject(updateDepartmentResult, ResponseUpdateDepartment.class);
        return updateDepartment;
    }

    @Override
    public ResponseDeleteDepartment delDepartment(Integer departmentId) {
        ResponseToken token = getAccessToken(WechatUtil.ADDRESS_SECRET);
        String requestUrl = WechatUtil.DELETE_DEPARTMENT_URL.replace("ACCESS_TOKEN", token.getAccess_token()).replace("DEPARTMENT_ID", departmentId + "");
        String departmentDeleteResult = HttpsUtils.httpsGet(requestUrl);
        ResponseDeleteDepartment departmentDelete = JSONObject.parseObject(departmentDeleteResult, ResponseDeleteDepartment.class);
        return departmentDelete;
    }

}
